package com.example.paranoid.createw;



import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.lang.reflect.Array;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {



    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.btn_dialog1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSimpleDialog();
            }
        });
        findViewById(R.id.btn_dialog2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showChooseDialog();
            }
        });
        findViewById(R.id.btn_dialog3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showClickDialog();
            }
        });
        findViewById(R.id.btn_dialog4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showMultipleDialog();
            }
        });
        findViewById(R.id.btn_dialog5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showEnterDialog();
            }
        });
    }

    private void showEnterDialog() {
        final EditText editText1 = new EditText(MainActivity.this);
        final EditText editText2 = new EditText(MainActivity.this);

        View view = LayoutInflater.from(MainActivity.this)
                .inflate(R.layout.dialog_input,null,false);
        final EditText nameEdit = view.findViewById(R.id.edt_name);
        final EditText ageEdit = view.findViewById(R.id.edt_age);

        new AlertDialog.Builder(MainActivity.this)
                .setTitle("请输入你的姓名年龄")
                .setView(view)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int i) {
                        String name = nameEdit.getText().toString();
                        int age = Integer.parseInt(ageEdit.getText().toString());
                        Toast.makeText(MainActivity.this,"你的名字是"+name+"年龄是"+age,
                                Toast.LENGTH_LONG).show();
                    }
                })
                .create()
                .show();
    }

    private void showMultipleDialog() {
        final String [] hobby={"打球","唱歌","打游戏","看书","写代码"};
        final boolean isChecked[] = new boolean[5];
        new AlertDialog.Builder(MainActivity.this)
                .setTitle("请选择你的爱好")
                .setMultiChoiceItems(hobby, null, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i, boolean b) {
                        isChecked[i]=b;
                        if(b){
                        Toast.makeText(MainActivity.this,"你选择了"+hobby[i],
                                Toast.LENGTH_LONG).show();
                        }else{
                            Toast.makeText(MainActivity.this,"你取消了"+hobby[i],
                                    Toast.LENGTH_LONG).show();
                        }

                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        StringBuffer buffer = new StringBuffer();
                        for(int j = 0;j<isChecked.length;j++){
                            if(isChecked[j]){
                                buffer.append(hobby[j]);
                            }
                        }
                        Toast.makeText(MainActivity.this,"你的爱好有"+buffer,
                                Toast.LENGTH_LONG).show();
                    }
                })
                .create()
                .show();
    }

    private void showClickDialog() {
        final String[] sex = {"男","女","不告诉你"};
        new AlertDialog.Builder(MainActivity.this)
                .setTitle("请选择你的性别")
                .setSingleChoiceItems(sex, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        Toast.makeText(MainActivity.this,
                                "您的性别是"+sex[i],Toast.LENGTH_LONG).show();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {

                    }
                })

                .create()
                .show();
    }

    private void showChooseDialog() {
        final String[] cities = {"北京","上海","广州","深圳"};
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("请选择城市")
                .setItems(cities, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        Toast.makeText(MainActivity.this,"您居住在"+cities[i],Toast.LENGTH_LONG).show();
                    }
                })
                .create()
                .show();
    }

    private void showSimpleDialog() {
         new AlertDialog.Builder(MainActivity.this)
                 .setIcon(R.mipmap.ic_launcher)
                 .setTitle("提示")
                 .setMessage("let's part")
                 .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                     @Override
                     public void onClick(DialogInterface dialog, int which) {
                         Toast.makeText(MainActivity.this,"ok",Toast.LENGTH_LONG).show();
                     }
                 })
                 .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                     @Override
                     public void onClick(DialogInterface dialog, int which) {
                         Toast.makeText(MainActivity.this,"what?",Toast.LENGTH_LONG).show();
                     }
                 })
                 .setNeutralButton("无所谓", new DialogInterface.OnClickListener() {
                     @Override
                     public void onClick(DialogInterface dialog, int which) {
                         Toast.makeText(MainActivity.this,"so?",Toast.LENGTH_LONG).show();

                     }
                 })
                 .create()
                 .show();

    }

}
